/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#include "tcp_worker.h"
#include "node/assert.h"
#include "node/system/console.h"
using namespace ::node::system;

namespace node {
    namespace net {

        TcpWorker::TcpWorker(void)
            : ebase_(NULL)
            , edummy_(NULL)
            , load_(0)
        {

        }

        TcpWorker::~TcpWorker(void)
        {
            if (edummy_) {
                event_free(edummy_);
                edummy_ = NULL;
            }
            if (ebase_) {
                event_base_free(ebase_);
                ebase_ = NULL;
            }
        }

        void TcpWorker::stop(void)
        {
            NODE_ASSERT(ebase_);
            event_base_loopbreak(ebase_);
        }

        bool TcpWorker::start(void)
        {
            do 
            {
                ebase_ = event_base_new();
                if (!ebase_) {
                    console::error("[TcpWorker::Start] Couldn't create an event_base\n");
                    break;
                }

                edummy_ = event_new(ebase_, -1, EV_TIMEOUT | EV_PERSIST,
                    &TcpWorker::onTimeout, this);
                if (!edummy_) {
                    console::error("[TcpWorker::Start] Couldn't create an event\n");
                    break;
                }
                struct timeval tv = { 1, 0 };
                if (0 != event_add(edummy_, &tv)) {
                    console::error("[TcpWorker::Start] Add dummy event failed\n");
                    break;
                }

                if (!::node::system::Thread::start()) {
                    break;
                }

                return true;
            } while (false);

            if (edummy_) {
                event_free(edummy_);
                edummy_ = NULL;
            }
            if (ebase_) {
                event_base_free(ebase_);
                ebase_ = NULL;
            }

            return false;
        }

        void TcpWorker::run(void)
        {
            NODE_ASSERT(ebase_);
            event_base_dispatch(ebase_);
        }

        void TcpWorker::onTimeout(evutil_socket_t, short, void *)
        {

        }

    }
}